package controller

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"my_pro/model"
	"my_pro/schemas"
)

// 总路由分发
func News(userGrp *gin.RouterGroup) {
	userGrp.Use().POST("/addNews", addNews)
	userGrp.Use().POST("/addAudit", addAudit)
	userGrp.Use().GET("/getNews", getNews)
	//userGrp.Use().GET("/getAudit", getAudit)
}

// 添加新闻
func addNews(c *gin.Context) {
	news := &schemas.News{}
	_ = c.Bind(news)

	stu := &model.News{
		Title:   news.Title,
		Content: news.Content,
		Userid:  news.Userid,
		Audit:   news.Audit,
		Type:    news.Type,
	}
	model.DB.Create(stu)

	c.JSON(200, gin.H{
		"msg": "添加成功",
	})
}

// 添加审核员
func addAudit(c *gin.Context) {
	audit := &schemas.Audit{}
	_ = c.Bind(audit)

	stu := &model.Audit{
		Name: audit.Name,
		Type: audit.Type,
	}
	model.DB.Create(stu)

	c.JSON(200, gin.H{
		"msg": "添加成功",
	})
}

// 获取没有审批的新闻
func getNews(c *gin.Context) {
	var stu []schemas.News
	var add []schemas.Audit

	sql := "select * from News where type=0"
	model.DB.Raw(sql).Scan(&stu)
	fmt.Println(stu)

	sql1 := "select * from Audits where type=1"
	model.DB.Raw(sql1).Scan(&add)
	fmt.Println(add)

	for _, v := range stu {
		id := v.Id % len(add)
		fmt.Println(id, ">>>>>>>>>>>>>>>>>>>>>>>>")
	}

	c.JSON(200, gin.H{"method": "GET", "data": stu, "date": add})
}
